package com.bridge.controller;

import com.bridge.entity.Brand;
import com.bridge.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author bridge
 * @Date 2022/06/16/21:15
 */
@Controller
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/")
    public String index() {
        return "brand/index";
    }

    @GetMapping("/start.html")
    public String start(Model model) {
        List<Brand> list = brandService.queryList();
        model.addAttribute("list", list);
        return "brand/brand";//第一个brand为目录,第二brand为view name
    }


    @GetMapping(value = {"/brand/doFindBrands/{name}", "/brand/doFindBrands"})
    public String doFindBrands(@PathVariable(required = false) String name, Model model) {
        List<Brand> list = brandService.findBrands(name);
        model.addAttribute("list", list);
        return "brand/brand";//第一个brand为目录,第二brand为view name
    }

    @GetMapping("/brand/doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id, Model model) {
        brandService.deleteById(id);
        List<Brand> list = brandService.queryList();
        model.addAttribute("list", list);
        return "brand/brand";
    }

    @GetMapping("/brand/doAddUI")
    public String doAddUI() {
        return "brand/brand-add";
    }

    @PostMapping("/brand/doSaveBrand")
    public String doSaveBrand(Brand brand, Model model) {
        System.out.println("save.brand=" + brand);
        brandService.saveBrand(brand);
        List<Brand> list = brandService.queryList();
        model.addAttribute("list", list);
        return "brand/brand";
    }

    @RequestMapping("/brand/doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model) {
        Brand brand = brandService.findById(id);
        model.addAttribute("brand", brand);
        return "/brand/brand-update";
    }

    @RequestMapping("/brand/doUpdateBrand")
    public String doUpdateBrand(Brand brand, Model model) {
        brandService.updateBrand(brand);
        List<Brand> list = brandService.queryList();
        model.addAttribute("list", list);
        return "brand/brand";
    }


}
